package com.example.sixtrainingdemo.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.transition.Slide;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.GoodsPhotoAdapter;
import com.example.sixtrainingdemo.adapter.RoundPhotoAdapter;
import com.example.sixtrainingdemo.contract.AddGoodsContract;
import com.example.sixtrainingdemo.contract.AddRoundContract;
import com.example.sixtrainingdemo.entity.Goods;
import com.example.sixtrainingdemo.entity.Photo;
import com.example.sixtrainingdemo.entity.Round;
import com.example.sixtrainingdemo.entity.RoundPhoto;
import com.example.sixtrainingdemo.presenter.AddGoodsPresenter;
import com.example.sixtrainingdemo.presenter.AddRoundPresenter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AddRoundActivity extends AppCompatActivity implements
        RoundPhotoAdapter.OnPhotoListener, AddRoundContract.IAddRoundView, View.OnClickListener {

   private RecyclerView mPhotoRecyclerView;
   private EditText mmessageEdit;

   private Button mSendButton;
   private List<RoundPhoto> mRoundPhotoList;
   private RoundPhotoAdapter mRoundPhotoAdapter;
   private ActivityResultLauncher mActivityResultLauncher;
   private AddRoundContract.IAddRoundPresenter mAddRoundPresenter;

   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().setEnterTransition(new Slide());
      setContentView(R.layout.activity_add_round);
      mAddRoundPresenter = new AddRoundPresenter(this, this);
      mRoundPhotoList = new ArrayList<>();
      mRoundPhotoList.add(new RoundPhoto());
      mPhotoRecyclerView = findViewById(R.id.rv_goods_photo);
      mSendButton = findViewById(R.id.round_send);
      mmessageEdit = findViewById(R.id.et_round);
      mSendButton.setOnClickListener(this);
      GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
      gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
      mPhotoRecyclerView.setLayoutManager(gridLayoutManager);
      mRoundPhotoAdapter = new RoundPhotoAdapter(mRoundPhotoList);
      mRoundPhotoAdapter.setPhotoListener(this);
      mPhotoRecyclerView.setAdapter(mRoundPhotoAdapter);
      onActivityResult();
   }

   @Override
   public void onAdd() {
      Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      mActivityResultLauncher.launch(intent);
   }

   @Override
   public void onDelete(int position) {
      //暂时没用
   }

   private void onActivityResult() {
      mActivityResultLauncher = registerForActivityResult(
              new ActivityResultContracts.StartActivityForResult(),
              new ActivityResultCallback<ActivityResult>() {
                 @Override
                 public void onActivityResult(ActivityResult result) {
                    int resultCode = result.getResultCode();
                    if (resultCode == RESULT_OK) {
                       Intent data = result.getData();
                       Uri selectedImageUri = data.getData();
                       try {
                          // 使用图片URI进行处理，例如加载到ImageView中
                          Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
                          RoundPhoto roundphoto = new RoundPhoto();
                          roundphoto.setBitmap(bitmap);
                          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                          Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.1), (int)(bitmap.getHeight()*0.1), true);
                          resized.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                          roundphoto.setData(outputStream.toByteArray());
                          mRoundPhotoAdapter.addRoundPhoto(roundphoto);
                       } catch (IOException e) {
                          e.printStackTrace();
                       }
                    }
                 }
              });
   }

   @Override
   public void showRoundList(List<Round> roundList) {

   }




   @Override
   public void onClick(View v) {
      Round round = new Round();
////      goods.setName(mGoodsNameEdit.getText().toString());
////      goods.setPrice(mpriceEdit.getText().toString());
//      try {
//         String message = mmessageEdit.getText().toString(); // 直接使用字符串
//         round.setMessage(message); // 将字符串传递给 setMessage 方法
//      } catch (NumberFormatException e) {
//         // 处理异常，例如显示错误信息给用户
//         e.printStackTrace();
//      }
      round.setMessage(mmessageEdit.getText().toString());
      round.setPhotoList(mRoundPhotoAdapter.getPhotoList());
      mAddRoundPresenter.saveRound(round);
      Toast.makeText(this, "添加成功,可继续添加", Toast.LENGTH_SHORT).show();
   }
}
